package com.cbtx.module_sp.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.cbtx.module.base.ui.BaseFragmentPagerAdapter
import com.cbtx.module.base.ui.BaseVMActivity
import com.cbtx.module.common.bean.BannerBean
import com.cbtx.module_sp.R
import com.cbtx.module_sp.ui.fragment.AdvImageFragment
import com.cbtx.module_sp.ui.fragment.AdvVideoFragment
import com.cbtx.module_sp.ui.vm.AdvActivityVM
import com.txcb.lib.base.utils.LogUtil
import kotlinx.android.synthetic.main.sp_activity_adv.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

//广告页面
class AdvActivity2: BaseVMActivity<AdvActivityVM>() {


    val fragmentList:MutableList<Fragment> = ArrayList()
    var mVpAdapter:BaseFragmentPagerAdapter? = null

    companion object{
        fun start(activity: Activity?,bannerItemList:ArrayList<BannerBean.BannerItem>){
            val intent = Intent(activity,
                AdvActivity2::class.java)
            intent.putExtra("bannerItemList",bannerItemList)
            activity?.startActivity(intent)
        }
    }

    override fun getPresenterClazz(): Class<AdvActivityVM> {
        return AdvActivityVM::class.java
    }

    override fun getLayoutId(): Int {

        return R.layout.sp_activity_adv
    }

    override fun initView(savedInstanceState: Bundle?) {

        mVM.getIntent(intent)
//        mVM.requestBanner()

        mVM.bannerItemList.observe(this, Observer {
            initViewPager()
        })

        EventBus.getDefault().register(this)
    }

    override fun onMyViewClick(view: View) {

    }

    override fun getData() {

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        finish()
        return super.onTouchEvent(event)
    }

    private fun initViewPager(){
        val bannerList = mVM.bannerItemList.value
        for(bannerItem in bannerList!!){
            if (bannerItem == null){
                continue
            }

            LogUtil.d("mVideoUrl:${bannerItem.localPath}")

            if (bannerItem.bannerType == 1){
               // 图片
               val advImageFragment = AdvImageFragment(bannerItem.localPath)
                fragmentList.add(advImageFragment)
            }
//            else if (bannerItem.bannerType == 2){
//                //视频

//                val advVideoFragment = AdvVideoFragment(bannerItem.bannerType,bannerItem.localPath)
//                fragmentList.add(advVideoFragment)
//            }
        }


        mVpAdapter = BaseFragmentPagerAdapter(supportFragmentManager,fragmentList)
        mViewPager.adapter = mVpAdapter

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onNextAdv(nextAdvEven: String){
        if ("nextAdv".equals(nextAdvEven)){
            var newPosition = mViewPager.currentItem +1
            if (newPosition >= fragmentList.size){
                newPosition =0
            }
            mViewPager.currentItem = newPosition

        }
    }

}